import json

import boto3

from framework.common.pt_logger import logger

events = boto3.client('events')


class EventManager(object):
    @staticmethod
    def pt_spider_state_changed(state, metadata, payload):
        detail = {
            "state": state,
            "metadata": metadata,
            "payload": payload
        }
        logger.info("sending event...")
        put_events_resp = events.put_events(
            Entries=[
                {
                    "Source": "pt.spider",
                    "DetailType": "Spider State-change Notification",
                    "Detail": json.dumps(detail)
                },
            ]
        )
        logger.info(put_events_resp)
        logger.info("send event done")
